package de.koller.million.render;

import de.koller.million.render.obj.LightRender;
import de.koller.million.render.obj.environment.Environment;
import de.koller.million.render.szene.Szene;
import de.koller.million.render.util.BufferContainer;
import de.koller.million.render.util.GUI;
import de.koller.oceanFramework.gl.util.InputGL;
import de.koller.oceanFramework.gl.util.ViewUtil;
import de.koller.oceanFramework.tree.Child;
import de.koller.oceanFramework.tree.Sea3X;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.GL11;

public class RenderRoot extends Sea3X {
	
	private int renderIteration = 0;
	private int frameIter = 0;
	
	@Child public final Environment envi = new Environment();
	@Child public final InputGL input = new InputGL();
	@Child public final BufferContainer buffer = new BufferContainer();
	@Child public final Camera camera = new Camera();
	
	@Child public final LightRender light = new LightRender();
	@Child public final Szene szene = new Szene();
	
	@Child private final GUI gui = new GUI();
	@Child public final Screen screen = new Screen();
	
	@Override
	protected void render( int elapsedTime ) {
		super.render( elapsedTime );
		
		GL11.glEnable( GL11.GL_NORMALIZE );
		ViewUtil.initPerspective();
		camera.preTransform();
	}

	@Override
	public void postChildRender() {
		super.postChildRender();
		
		camera.postTransform();
		
		if( renderIteration == 2 ) {
			ViewUtil.readPixels( screen.getLightLayer() );
		} else if( renderIteration == 3 ) {
			renderIteration = 0;
			screen.saveFrame( frameIter );
			frameIter++;
		}
		
		if( renderIteration > 0)
			renderIteration++;
		if( input.isKeyPressed( Keyboard.KEY_R ) )
			renderIteration = 1;
	}

	public int getRenderIteration() {
		return renderIteration;
	}

	public void setRenderIteration( int renderIteration ) {
		this.renderIteration = renderIteration;
	}
	
}
